Populate List DBE with string array from function?

Here is a snippit of code that is causing an exception when the show(box) method is called:

string AAA()[]
{
  string x[] = {"1", "2", "3", "4", "5", "6"}
  return x
}
 
DB box = create "Hello" 
string items[]
items = AAA()
DBE mylist = list(box, "Choose:", 5, items) 
show box

 


If I replace the assignment of the string array from a function call to an immediate assignment, then it works:

 

 

DB box = create "Hello" 
string items[] = {"a", "b", "c"}
DBE mylist = list(box, "Choose:", 5, items) 
show box



Any ideas on what I am doing wrong?



 


SystemAdmin - Fri Oct 02 10:34:58 EDT 2009

Re: Populate List DBE with string array from function?
llandale - Fri Oct 02 11:04:49 EDT 2009

Curious, this works OK:

string AAA()[]
{
  string x[] = {"1", "2", "3", "4", "5", "6"}
  return x
}
 
string items[]
items = AAA()
int Size = sizeof(items)
int i
for (i=0; i<Size; i++)
{  print "\t" items[i]
}
print "\n"


I know there's a problem defining a list et.tal inside a function that has a local array declared, since the array is used when the box is realized/shown, and that array no longer exists since its in the stack of the exited function. I wish I had thought of your approach, which should work, but perhaps there's a problem.

My hokey solution is to declare items[] very large in the main, use it as a call parameter to a function that populates it and returns how many valid values there are, then issue the list() call.

 

 

  • Louie

 

 

Re: Populate List DBE with string array from function?
SystemAdmin - Fri Oct 02 11:51:15 EDT 2009

llandale - Fri Oct 02 11:04:49 EDT 2009

Curious, this works OK:

string AAA()[]
{
  string x[] = {"1", "2", "3", "4", "5", "6"}
  return x
}
 
string items[]
items = AAA()
int Size = sizeof(items)
int i
for (i=0; i<Size; i++)
{  print "\t" items[i]
}
print "\n"


I know there's a problem defining a list et.tal inside a function that has a local array declared, since the array is used when the box is realized/shown, and that array no longer exists since its in the stack of the exited function. I wish I had thought of your approach, which should work, but perhaps there's a problem.

My hokey solution is to declare items[] very large in the main, use it as a call parameter to a function that populates it and returns how many valid values there are, then issue the list() call.

 

 

  • Louie

 

 

Thanks for the reply, Louie.

You must be right - the array that is declared locally inside the function must be returned by reference - instead of a copy being returned. That doesn't completely explain why you can print the contents after the call returns but cannot use it in the list, except that it must still be sitting on the stack, unaltered for a short while after the return.

Using this I tried to create the list using a dynamic array instead, but of course the create list call will not accept dynamic arrays. It looks like I will also have to resort to your hokey approach. lol!

I will leave this question open for a little while to see if anyone else has any insight into this issue, and I'll posit this question as well: Would you consider this returning-the-array-by-reference behavior to be a bug in DXL? I would, as it makes returning arrays from functions valueless, and is certainly not the behavior I would expect.

Re: Populate List DBE with string array from function?
llandale - Fri Oct 02 15:52:05 EDT 2009

SystemAdmin - Fri Oct 02 11:51:15 EDT 2009
Thanks for the reply, Louie.

You must be right - the array that is declared locally inside the function must be returned by reference - instead of a copy being returned. That doesn't completely explain why you can print the contents after the call returns but cannot use it in the list, except that it must still be sitting on the stack, unaltered for a short while after the return.

Using this I tried to create the list using a dynamic array instead, but of course the create list call will not accept dynamic arrays. It looks like I will also have to resort to your hokey approach. lol!

I will leave this question open for a little while to see if anyone else has any insight into this issue, and I'll posit this question as well: Would you consider this returning-the-array-by-reference behavior to be a bug in DXL? I would, as it makes returning arrays from functions valueless, and is certainly not the behavior I would expect.

OK, I did just now try it by inserting into my 'curious' code a silly call to a silly function that deals with strings; after AAA and before printing of AAA. Yes, it gets diagonstic errors. Sure glad I didn't post my original thoughts before testing.....

I notice we get similar diagnostic errors when you indeed pass a vector to a function as a parameter, using the &, expecting it to get modified. Don't use it and it works.

Yup, looks like a bug to me.

Tried various forms of 'addr_', but not clever enough to make it work.

  • Louie

Re: Populate List DBE with string array from function?
Doug.Zawacki - Mon Oct 05 13:48:19 EDT 2009

Would this be a solution that would work for your situation?

DBE AAA(DB box)
{
 string x[] = {"1","2"}
 DBE d = list(box,"choose",sizeof(x),x)
 
return d
}
 
DB box = create "Hello"
DBE mylist = AAA(box)
show box

Re: Populate List DBE with string array from function?
SystemAdmin - Mon Oct 05 17:57:52 EDT 2009

Doug.Zawacki - Mon Oct 05 13:48:19 EDT 2009

Would this be a solution that would work for your situation?

DBE AAA(DB box)
{
 string x[] = {"1","2"}
 DBE d = list(box,"choose",sizeof(x),x)
 
return d
}
 
DB box = create "Hello"
DBE mylist = AAA(box)
show box

This is essentially what I did to work around the problem, but it lead to another problem with lists and arrays (see my other thread)

As memtioned in that other thread, the issue seems to have been resolved sometime before Doors 8.3